local skynet = require "skynet"
local httpc = require "http.httpc"
local filelog = require "filelog"
local is_set_dns = false

local Httpservice = {
	CMD = {},
}

function Httpservice.CMD.send_http_request(rid, request_url)
    filelog.sys_error("----Httpservice.CMD.send_http_request------", rid, request_url)
	local host,url = string.match(request_url,"://(.+%.%w+)/(.+)")
    local recvheader = { }
    url = "/" .. (url or "") 
    
    filelog.sys_info("---start http request---",rid, host, url)
    if not is_set_dns then
        skynet.pcall(httpc.dns) -- 异步dns服务，只设置一次
        is_set_dns = true
    end
	httpc.timeout = 300	        -- 超时3s
	-- if host == "api.daili.com" then
	-- 	host = "192.168.1.75"
	-- end
    local isok,status,body = skynet.pcall(httpc.post, host, url, {}, recvheader)
    filelog.sys_info("---end http request---", rid, status, body, host, url)
    if status == 200 and body then
        return body
    elseif not is_ok then
        skynet.pcall(httpc.dns)
		return false
    end
    return 
end

local function init()
end

skynet.start(function()
	skynet.dispatch("lua",function(session, source, cmd, ...)
        local f = Httpservice.CMD[cmd]
        if f then
            skynet.retpack(f(...))
        else
            skynet.retpack()
        end
    end)
    init()
end)